package single;

public class SinglePerson {
    private static boolean flag = true;
    private static volatile SinglePerson singlePerson;

    private SinglePerson() throws Exception {
        if (flag) {
            flag = false;
        } else {
            throw new Exception("单例模式被入侵!");
        }
    }

    public static SinglePerson getSinglePerson() throws Exception {
        if (singlePerson == null) {
            synchronized (SinglePerson.class) {
                if (singlePerson == null) {
                    singlePerson = new SinglePerson();
                }
            }
        }
        return singlePerson;
    }
}
